package com.cndongtai.tag;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.cndongtai.constant.SystemConstant;

public class MessageTag extends TagSupport{

	private static final long serialVersionUID = -1L;

    private PageContext pageContext = null;

    private String key = null;
    
    private ReloadableResourceBundleMessageSource messageSource;

	public void setKey(String key) {
		this.key = key;
	}

	public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
        messageSource = WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext()).getBean("messageSourceXX",ReloadableResourceBundleMessageSource.class);
    }

    public int doStartTag() throws JspException {
    	String language = (String) pageContext.getSession().getAttribute(SystemConstant.LANGUAGE);
    	Locale local = null;
    	if("zh".equals(language)) {
    		local = new Locale("zh","CN");
    	}else {
    		local = new Locale("en","US");
    	}
    	
        String message = "";
        if(key!=null && !"".equals(key)) {
        	message = messageSource.getMessage(key, new Object[]{}, local);
        }
        JspWriter out = pageContext.getOut();
        try {
            out.print(message);
        } catch (IOException e) {
        }
        return super.doStartTag();
    }
}
